<?php
class ControllerModuleDelivery extends Controller {
    protected function index() {
        $this->data['template'] = 'catalog/view/theme/'.$this->config->get('config_template').'/';

        $this->load->model('checkout/delivery');
        $this->data['deliveries'] = false; //$this->model_checkout_delivery->getDeliveries();
        $this->data['action_save'] = $this->url->link('module/delivery/save', '', 'SSL');
        $this->data['action_clear'] = $this->url->link('module/delivery/clear', '', 'SSL');
        $this->data['action_load'] = $this->url->link('module/delivery/load', '', 'SSL');

        $cookie_expires = (isset($_COOKIE['delivery']) && $_COOKIE['delivery'] == 1) ? false : true;
        $this->data['cookie_expires'] = $cookie_expires;
        if($cookie_expires){
            $this->data['postcode'] = '';
            $this->data['city'] = '';
            $this->data['price'] = '0';

            unset($this->session->data['delivery']);
        }
        else{
            $this->data['postcode'] = isset($this->session->data['delivery']['postcode']) ? $this->session->data['delivery']['postcode'] : '';
            $this->data['city'] = isset($this->session->data['delivery']['city']) ? $this->session->data['delivery']['city'] : '';
            $this->data['price'] = isset($this->session->data['delivery']['price']) ? $this->session->data['delivery']['price'] : '0';
        }

        $this->load->model('catalog/download');
        $download_info = $this->model_catalog_download->getDownload(1);
        $this->data['download'] = $this->url->link('module/download', 'download_id=' . $download_info['download_id']);

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/delivery.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/delivery.tpl';
        } else {
            $this->template = 'default/template/module/delivery.tpl';
        }

        $this->render();
    }

    public function save() {
        $data = array();

        if(($this->request->server['REQUEST_METHOD'] == 'POST')){
            $postcode = $this->request->post['delivery_postcode'];
            $city = $this->request->post['delivery_city'];
            $price = $this->request->post['delivery_price'];

            if($postcode && $city){
                $this->session->data['delivery']['postcode'] = $postcode;
                $this->session->data['delivery']['city'] = $city;
                $this->session->data['delivery']['price'] = $price;
            }
            else{
                unset($this->session->data['delivery']);
            }

            setcookie('delivery', 1, time()+3600);

            $data['success'] = true;
            $data['url'] = $this->data['category'] = $this->url->link('product/category', 'path=1', 'SSL');
        }
        else{
            $data['success'] = false;
            $data['url'] = '';
        }

        $this->response->setOutput(json_encode($data));
    }

    public function clear() {
        unset($this->session->data['delivery']);
        setcookie('delivery', 0, -1*(time()+3600));
    }

    public function load() {
        $data = array();
        if(($this->request->server['REQUEST_METHOD'] == 'POST')){
            $postcode = $this->request->post['delivery_postcode'];
            $method = isset($this->request->post['shipping_method']) ? $this->request->post['shipping_method'] : false;

            $this->load->model('checkout/delivery');
            $delivery = $this->model_checkout_delivery->getPostcode($postcode);
            if($delivery){
                $data['postcode'] = $delivery['postcode'];
                $data['city'] = $delivery['city'];
                $data['price'] = $delivery['price'];
            }
            else{
                $data['postcode'] = $postcode;
                $data['city'] = '';
                $data['price'] = 0;
            }

            if($method && isset($this->session->data['shipping_methods'][$method])){
                $cost = $this->cart->getSubTotal() >= 200 ? 0 : $data['price'];

                $this->session->data['shipping_methods'][$method]['cost'] = $cost;
                $this->session->data['shipping_methods'][$method]['text'] = $this->currency->format($cost);

                if(isset($this->session->data['shipping_methods'][$method]['quote'][$method])){
                    $this->session->data['shipping_methods'][$method]['quote'][$method]['cost'] = $cost;
                    $this->session->data['shipping_methods'][$method]['quote'][$method]['text'] = $this->currency->format($cost);
                }
            }
        }

        $result = array(
            'status'    => $data ? '1' : '0',
            'data'      => $data
        );

        $this->response->setOutput(json_encode($result));
    }
}
?>